import {
  reqAddUserAddress,
  reqHasUserAddress,
  reqUpdateUserInfo
} from '../../../api/index'
Page({
  data: {
    name: '', //收货人的姓名
    phone: '', //收货人的手机号码
    provinceCode: 0, //省邮政编码
    cityCode: 0, //市的邮政编码
    districtCode: 0, //区的邮政编码
    address: '', //详细的地址
    tagName: '家', //标签收集数据
    isDefault: 0, //默认地址
    showAddress: '', //选中的地址
    id: '', //已有收货人的ID
  },
  onLoad(option) {
    if (option.id) {
      this.setData({
        id: option.id
      });
      this.getHasUserInfo();
    }
  },
  fetchCode(event) {
    this.setData({
      provinceCode: event.detail.code[0],
      cityCode: event.detail.code[1],
      districtCode: event.detail.code[2],
      showAddress: event.detail.value.join('/')
    })
  },
  // 是否默认地址
  changeDefault(event) {
    this.setData({
      isDefault: event.detail.value ? 1 : 0
    })
  },
  // 保存
  async save() {
    await reqAddUserAddress(this.data);
    wx.navigateBack()
  },
  // 编辑操作
  async getHasUserInfo(){
    const result = await reqHasUserAddress(this.data.id);
    console.log(result);
    if (result.code == 200) {
      this.setData({
        name: result.data.name,
        phone: result.data.phone,
        provinceCode: result.data.provinceCode,
        cityCode: result.data.cityCode,
        districtCode: result.data.districtCode,
        showAddress: result.data.provinceName + '/' + result.data.cityName + '/' + result.data.districtName,
        address: result.data.fullAddress,
        tagName: result.data.tagName,
        isDefault: result.data.isDefault,
        id: result.data.id
      })
    }
  }
})